﻿#include "xict_ui_core/imageitem.h"
#include "xict_ui_core/vehiclemanage.h"
namespace xict_ui_core {

    ImageItem::ImageItem(QQuickItem* parent) : QQuickPaintedItem(parent) {
        settop_screen(false);
        setcut_num(1);
    }

    // void ImageItem::connectImageTarget()
    //{
    //   xrcu_cpp::VehicleManage::getInstance()->connectImageItem(this,
    //   image_type());
    //}

    void ImageItem::paintFiveSceenImage(int show_state, QImage img) {
        // qDebug() << "paintFiveSceenImage show_state" << show_state << "
        // m_image_index" << m_image_index;
        if (m_cut_num > 1) {
            if (show_state < 0 && m_image_index < 6)   // 显示开机动画
            {
                int sub_width = img.width() / m_cut_num;
                // m_image = img.copy((m_image_index - 1) * sub_width + 1, 1200,
                //                    sub_width - 1, 1080);
                m_image = img.copy((m_image_index - 1) * sub_width + 1, 1080,
                                   sub_width - 1, 1920);

            } else if (show_state < 0 && m_image_index == 6) {
                m_image = img.copy(1740, 1080 + 1920, 1920, 1080);
            } else if (show_state > 0 && m_image_index == 2) {
                int sub_width = img.width() / 3;
                m_image       = img.copy((m_image_index - 2) * sub_width + 1, 0,
                                         sub_width - 1, img.height());
            }
            if (show_state > 0 && m_image_index == 3) {
                int sub_width = img.width() / 3;
                m_image       = img.copy((m_image_index - 2) * sub_width + 1, 0,
                                         sub_width - 1, img.height());
            }
            if (show_state > 0 && m_image_index == 4) {
                int sub_width = img.width() / 3;
                m_image       = img.copy((m_image_index - 2) * sub_width + 1, 0,
                                         sub_width - 1, img.height());
            }
        } else {
            if (m_revolve) {
                QMatrix matrix;
                if (show_state == 1) {
                    matrix.rotate(90.0);
                } else {
                    matrix.rotate(-90.0);
                }
                QImage tmp_img = img.transformed(matrix);
                m_image        = tmp_img.copy();
            } else {
                m_image = img.copy();
            }
        }

        update();
    }

    void ImageItem::paintTwoOneTwoImage(int show_state, QImage img) {
        if (m_cut_num > 1) {
            if (show_state == -1) {
                if (m_top_screen) {   // 显示开机动画
                    m_image = img.copy((img.width() - 1920) / 2, 0, 1920, 1080);
                } else {
                    int sub_width = img.width() / 5;
                    if (m_image_index == 1) {
                        m_image = img.copy(1, 1080, sub_width * 2 - 1, 1920);
                    } else if (m_image_index == 2) {
                        m_image =
                            img.copy(sub_width * 2, 1080, sub_width - 1, 1920);
                    } else if (m_image_index == 3) {
                        m_image = img.copy(sub_width * 3, 1080,
                                           sub_width * 2 - 1, 1920);
                    }
                }
            } else {
                if (m_top_screen) {
                    m_image = img.copy();
                } else if (show_state > 0) {
                    //                int sub_width = img.width() / m_cut_num;
                    //                m_image = img.copy(m_image_index *
                    //                sub_width + 10, 0, sub_width - 10,
                    //                img.height());
                    //                //显示实时图像
                    int sub_width = img.width() / 5;
                    if (m_image_index == 1) {
                        m_image =
                            img.copy(10, 0, sub_width * 2 - 10, img.height());
                    } else if (m_image_index == 2) {
                        m_image = img.copy(sub_width * 2 + 10, 0,
                                           sub_width - 10, img.height());
                    } else if (m_image_index == 3) {
                        m_image = img.copy(sub_width * 3 + 10, 0,
                                           sub_width * 2 - 10, img.height());
                    }
                } else {
                    int sub_width = img.width() / 3;
                    if (m_image_index == 1) {
                        QImage src_img =
                            img.copy(10, 0, sub_width - 10, img.height());
                        QImage temp_img(src_img.size().width() * 2,
                                        src_img.size().height(),
                                        src_img.format());
                        temp_img.fill(QColor(Qt::black));
                        QPainter painter(&temp_img);
                        painter.fillRect(temp_img.rect(), Qt::transparent);
                        painter.drawImage(src_img.size().width(), 0, src_img);
                        m_image = temp_img.copy();
                    } else if (m_image_index == 2) {
                        m_image = img.copy(sub_width + 10, 0, sub_width - 10,
                                           img.height());
                    } else if (m_image_index == 3) {
                        m_image = img.copy(sub_width * 2 + 10, 0,
                                           sub_width * 2 - 10, img.height());
                    }
                }
            }
        } else {
            if (m_revolve) {
                QMatrix matrix;
                if (show_state == 1) {
                    matrix.rotate(90.0);
                } else {
                    matrix.rotate(-90.0);
                }
                QImage tmp_img = img.transformed(matrix);
                m_image        = tmp_img.copy();
            } else {
                m_image = img.copy();
            }
        }

        update();
    }

    void ImageItem::paintMainFiveSceenImage(int show_state, QImage img) {
        // qDebug() << "paintMainFiveSceenImage show_state" << show_state << "
        // m_image_index" << m_image_index;
        if (show_state > 0) {
            int sub_width = img.width() / 3;
            m_image       = img.copy((m_image_index - 2) * sub_width + 1, 0,
                                     sub_width - 1, img.height());
        } else {
            m_image = img;
        }
        update();
    }
    void ImageItem::paint(QPainter* painter) {
        if (m_image.width() > 0)
            painter->drawImage(QRect(0, 0, this->width(), this->height()),
                               m_image);
    }

}   // namespace xict_ui_core
